package com.lightsh.contact;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

public class ContactConstants {	
	// 联系人信息
	private static final String[] selectCol = new String[]{  
	        ContactsContract.Contacts.DISPLAY_NAME,  
	        ContactsContract.Contacts.HAS_PHONE_NUMBER,  
	        ContactsContract.Contacts._ID  
	    };  
	  
	// 电话信息
	private static final String[] selPhoneCols = new String[] {  
	        ContactsContract.CommonDataKinds.Phone.NUMBER,  
	        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,  
	        ContactsContract.CommonDataKinds.Phone.TYPE  
	};
	
	/**
	 * FIXME 效率问题
	 * @param context
	 * @return
	 */
	public static final List<ContactInfo> getAllContacts(Context context){
		ArrayList<ContactInfo> list = new ArrayList<ContactInfo>();
		Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, selectCol, null, null, null);		
		try{
			if(cursor.moveToFirst()){
				int nameCulumn = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
				int idCulumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
				int hasColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
				
				String name;
				long id;
				int phoneCount;
				do{
					name = cursor.getString(nameCulumn);
					id = cursor.getLong(idCulumn);
					phoneCount = cursor.getInt(hasColumn);
					if(phoneCount > 0){
						 Cursor phoneCursor = context.getContentResolver().query(  
			                       ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  
			                       selPhoneCols,  
			                       ContactsContract.CommonDataKinds.Phone.CONTACT_ID  
			                               + "=" + id, null, null);
						 try{
							 if(phoneCursor.moveToFirst()){
								 ArrayList<PhoneInfo> phoneList = new ArrayList<PhoneInfo>();								 
								 int numberColumn = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
								 int typeColumn = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);								 
								 String phoneString;
								 int phoneType;
								 do{
									 phoneString = phoneCursor.getString(numberColumn);
									 phoneType = phoneCursor.getInt(typeColumn);
									 phoneList.add(new PhoneInfo(phoneString, phoneType, name));									 
								 }while(phoneCursor.moveToNext());								 
								 ContactInfo info = new ContactInfo(id, name, phoneList);
								 list.add(info);
							 }
						 }finally{
							 if(phoneCursor != null){
								 phoneCursor.close();
							 }
						 }						 
					}else{
						log(name + " has no phone number");
					}
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(cursor != null){
				cursor.close();
			}
		}
		return list;
	}
	
	
	private static final void log(String tip){
		Log.d(ContactConstants.class.getName(), "" + tip);
	}
}
